/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.tree;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import jpcap.packet.Packet;
import org.jdesktop.swingx.renderer.DefaultTreeRenderer;
import tnviet.proj.jna.JNPacketAnalyzerLoader;
import tnviet.proj.jna.analyzer.JNPacketAnalyzer;

/**
 *
 * @author Administrator
 */
public class JNTreePane extends JPanel {

    JTree tree;
    JScrollPane treeViewPane;

    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    JNPacketAnalyzer[] analyzers = JNPacketAnalyzerLoader.getAnalyzers();
    Icon icon = null;

    public JNTreePane() {
        tree = new JTree(root);
        tree.setRootVisible(false);
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
        renderer.setLeafIcon(icon);
        treeViewPane = new JScrollPane(tree);
        this.setLayout(new BorderLayout());
        this.add(treeViewPane, BorderLayout.CENTER);
        //this.setMinimumSize(new Dimension(300, 400));
        this.setMinimumSize(new Dimension(0, 0));
    }

    private void addNode(DefaultMutableTreeNode node,String str) {
        node.add(new DefaultMutableTreeNode(str));
    }
    private void addNodes(DefaultMutableTreeNode node, String str, Vector v) {
        DefaultMutableTreeNode subNode = new DefaultMutableTreeNode(str);
        for(int i = 0;i<v.size(); i++) {
            subNode.add(new DefaultMutableTreeNode(v.elementAt(i)));
        }
        node.add(subNode);
    }

    public void AnalyzePacket(Packet packet){
        boolean[] isExpand = new boolean[root.getChildCount()];
        for(int i=0; i<root.getChildCount(); i++){
            isExpand[i] = tree.isExpanded(
                    new TreePath(((DefaultMutableTreeNode)root.getChildAt(i)).getPath()));
        }
        root.removeAllChildren();
        DefaultMutableTreeNode node;
        for(int i=0; i<analyzers.length; i++){
            if(analyzers[i].isAnalyzable(packet)){
                analyzers[i].analyze(packet);
                node = new DefaultMutableTreeNode(analyzers[i].getProtocolName());
                root.add(node);
                String[] names = analyzers[i].getValueNames();
                Object[] values = analyzers[i].getValues();
                if (names == null) continue;
                for(int j=0; j<names.length; j++){
                    if(values[j] instanceof Vector){
                        addNodes(node, names[j] , (Vector)values[j]);
                    } else if(values[j] != null){
                        addNode(node, names[j] + ": " + values[j]);
                    }
                }
            }
        }
        ((DefaultTreeModel)tree.getModel()).nodeStructureChanged(root);
        for(int i=0; i< Math.min(root.getChildCount(), isExpand.length); i++){
            if(isExpand[i]){
                tree.expandPath(new TreePath(
                        ((DefaultMutableTreeNode)root.getChildAt(i)).getPath()));
            }
        }
        tree.repaint();
    }
}
